package com.lanou.musicbox.adapter.onlinemusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.Singer;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 柳柏庆 on 15/10/24.
 * 网络音乐_歌手页面Adapter
 */
public class SingersListAdapter extends BaseAdapter {
    private List<Singer.DataEntity> dataEntities;
    private Context context;
    private ImageLoader imageLoader;


    /***
     * 无参的构造方法
     */
    public SingersListAdapter(Context context, List<Singer.DataEntity> data) {
        this.context = context;
        this.dataEntities = data;
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();

/**
 *行数量的回调方法
 *  **/
    }

    @Override
    public int getCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    /**
     * 行下标的回调方法
     */
    @Override
    public Object getItem(int position) {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.get(position) : 0;
    }

    /**
     * 行下标id的回调方法
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * position 行下标
     * converView 行布局内容
     * parent列表视图
     ***/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.singers_item, null);
            viewHolder = new ViewHolder();
            viewHolder.pic_url = (CircleImageView) convertView.findViewById(R.id.singer_image);
            viewHolder.singer_name = (TextView) convertView.findViewById(R.id.singer_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.singer_name.setText(dataEntities.get(position % dataEntities.size()).getSinger_name());
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.pic_url, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(dataEntities.get(position % dataEntities.size()).getPic_url(), imageListener);
        return convertView;
    }

    class ViewHolder {
        private CircleImageView pic_url;
        private TextView singer_name;

    }

    public int getUrl(int position) {
        return dataEntities.get(position).getSinger_id();

    }

}
